package io.github.novaframe.quartz;

import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;

import java.io.IOException;

/**
 * Created by Locki on 9/7/2017.
 */
@Slf4j
public class QuartzJob implements Job {

    public final static String JOB_DATA_KEY = "job_data_key";

    ObjectMapper objectMapper = new ObjectMapper();

    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        String jobStr = jobExecutionContext.getMergedJobDataMap().getString(JOB_DATA_KEY);
        QuartzJobModel quartzJobVM = null;
        try {
            quartzJobVM = objectMapper.readValue(jobStr, QuartzJobModel.class);
        } catch (IOException e) {
            log.error("parse json error: {}", e.getMessage());
        }

        log.info("Quartz execute task name = {}", quartzJobVM.getJobName());
        QuartzJobUtil.invokMethod(quartzJobVM);
    }
}
